/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.orderView;
/**
 * 说明：
 * 订单详细
 * @Author：黄盛强
 * @Date：2012-03-26
 */
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.util.List;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;

import com.ruipengkj.ApplicationContext;
import com.ruipengkj.bean.TBillDetail;
import com.ruipengkj.bean.TOrder;
import com.ruipengkj.bean.TShopMemberLevel;
import com.ruipengkj.bean.TUser;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.component.jdialog.BorderJPanel;
import com.ruipengkj.component.jdialog.IDialog;
import com.ruipengkj.component.jpanel.RepeatImagePanel;
import com.ruipengkj.factory.ImageFactory;
import com.ruipengkj.model.OrderDetailModel;
import com.ruipengkj.service.TBillDetailService;
import com.ruipengkj.service.TCommodityService;
import com.ruipengkj.service.TOrderService;

public class OrderDetailDialog extends IDialog {

	private static final long serialVersionUID = 1L;
	
	private JLabelJTextFieldComponent cartIDTF = null; //会员卡号
	private JLabelJTextFieldComponent telTF = null; //手机号
	
	private JLabelJTextFieldComponent OrderNumTF = null;//订单号
	private JLabelJTextFieldComponent source;//订单来源
	
	private JTable table = null;//订单Table
	private OrderDetailModel OrderDetailModel;//model
	private JScrollPane tableJsp;
	private JLabelJTextFieldComponent totalMoneyTF=null;//总金额
	private JLabelJTextFieldComponent receivableMoneyTF=null;//应收金额
	private JLabelJTextFieldComponent discountTF=null;//折扣
	private JLabelJTextFieldComponent privilegeTF=null;//优惠
	private JLabelJTextFieldComponent menberLevelTF=null;//会员级别
	private JLabelJTextFieldComponent employeesTF;//操作员
	
	private TBillDetailService tBillDetailService;//单据商品明细
	private TCommodityService tCommodityService;//商品
	private TOrderService tOrderService;//订单
	
	public OrderDetailDialog(JDialog jd, String title, boolean b) {
		super(jd, title, b);
		tOrderService = BuildSpringApplication.getInstance().getTOrderServiceImpl();
		tBillDetailService = BuildSpringApplication.getInstance().getTBillDetailServiceImpl();
		tCommodityService = BuildSpringApplication.getInstance().getTCommodityServiceImpl();
		this.init();
		this.setSize(900, 600);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		
	}
	private void init() {
		OrderDetailModel = new OrderDetailModel();
		table=uiFactory.createRPJTable(OrderDetailModel, this, this, 30, JLabel.CENTER);
//		Util.hideTableColumn(0, table);
//		Util.hideTableColumn(1, table);
//		Util.hideTableColumn(2, table);
		tableJsp=uiFactory.createJScrollPane(table);
		
		JPanel main_jp=new BorderJPanel(new BorderLayout());
		main_jp.setBackground(Util.PANEL_COLOR);
		main_jp.add(this.topPanel(), BorderLayout.NORTH);
		main_jp.add(tableJsp, BorderLayout.CENTER);
		main_jp.add(this.downMainPanel(), BorderLayout.SOUTH);
		main_jp.setCursor(Util.CURSOR_DEFAULT);
		
		this.add(main_jp);
	}

	//头部面板
	private JPanel topPanel(){
		JPanel p1 = new RepeatImagePanel(ImageFactory.loadImage(ImageFactory.DIALOG_TOP_BG_1).getImage());
		p1.setLayout(new FlowLayout(FlowLayout.LEFT));
		OrderNumTF = new JLabelJTextFieldComponent(" 订 单 号：",20);
		cartIDTF = new JLabelJTextFieldComponent("会员卡号：",20);
		telTF = new JLabelJTextFieldComponent("手机号：",20);
		source = new JLabelJTextFieldComponent("订单来源", 20);
		
		OrderNumTF.getText().setEditable(false);
		cartIDTF.getText().setEditable(false);
		telTF.getText().setEditable(false);
		source.getText().setEditable(false);
		
		p1.add(OrderNumTF);
		p1.add(cartIDTF);
		p1.add(telTF);
		p1.add(source);
		return p1;
	}

	private JPanel downMainPanel(){
		JPanel jp = new RepeatImagePanel(ImageFactory.loadImage(ImageFactory.DIALOG_DOWN_BG_1).getImage());
		jp.setLayout(new FlowLayout(FlowLayout.LEFT));
		employeesTF = new JLabelJTextFieldComponent("操作员：", 15);
		employeesTF.getText().setText(ApplicationContext.OPERATOR_NAME);
		employeesTF.getText().setEditable(false);
		receivableMoneyTF=new JLabelJTextFieldComponent("应收金额：", 10);
		totalMoneyTF=new JLabelJTextFieldComponent("总金额：",10);
		discountTF=new JLabelJTextFieldComponent("折扣：", 5);
		privilegeTF=new JLabelJTextFieldComponent("优惠：", 5);
		menberLevelTF=new JLabelJTextFieldComponent("会员级别：", 10);
		receivableMoneyTF.getText().setEditable(false);
		totalMoneyTF.getText().setEditable(false);
		discountTF.getText().setEditable(false);
		privilegeTF.getText().setEditable(false);
		menberLevelTF.getText().setEditable(false);
		receivableMoneyTF.getText().setText("0.00");
		totalMoneyTF.getText().setText("0.00");
		discountTF.getText().setText("无");
		privilegeTF.getText().setText("0.00");
		menberLevelTF.getText().setText("无");
		jp.add(employeesTF);
		jp.add(receivableMoneyTF);
		jp.add(totalMoneyTF);
		jp.add(discountTF);
		jp.add(privilegeTF);
		jp.add(menberLevelTF);
		return jp;
	}
	
	//数据库中的数据放入list（读取数据）
	private Runnable queryBillDetail=new Runnable() {
		@Override
		public void run() {
			try {
				TBillDetail tBillDetail = new TBillDetail();
				tBillDetail.setBillID(OrderNumTF.getText().getText());
				tBillDetail.setBillType(ApplicationContext.BILL_BILLTYPE3);
				List<TBillDetail> billDetailList = tBillDetailService.findService(tBillDetail);
				if (billDetailList != null &&  billDetailList.size()!= 0) {
					int length = billDetailList.size();
					for (int i = 0; i < length; ++i) {
						//获取商品 以便查看
//						System.out.println(length+"     "+billDetailList.get(i).getCommodityID());
						billDetailList.get(i).setCommodity(tCommodityService.getByIdService(billDetailList.get(i).getCommodityID()));
						OrderDetailModel.addBillDetail(billDetailList.get(i));
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	};
	
	public void showDialog(String orderID) {
		TOrder tOrder = null;
		try {
			tOrder = tOrderService.getByIdService(orderID);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (tOrder != null) {
			SwingUtilities.invokeLater(queryBillDetail);
			OrderNumTF.getText().setText(orderID);
			employeesTF.getText().setText(tOrder.getOperator());
			receivableMoneyTF.getText().setText(tOrder.getNeedMoney()+"");
			totalMoneyTF.getText().setText(tOrder.getTotalAmount()+"");
			TUser user=tOrder.getTUser();
			if (user != null) {
				TShopMemberLevel tShopMemberLevel = user.getTShopMemberLevel();
				if (user.getTShopMemberLevel() != null) {
					discountTF.getText().setText(tShopMemberLevel.getDiscount()+"");//折扣
					privilegeTF.getText().setText(tOrder.getTotalAmount()-tOrder.getNeedMoney()+"");//优惠
					menberLevelTF.getText().setText(tShopMemberLevel.getName());//会员级别
				}
				cartIDTF.getText().setText(user.getCardNum()); //会员卡号
				telTF.getText().setText(user.getMobile()); //手机号
			}
			source.getText().setText(ApplicationContext.ORDER_SOURCE_MAP().get(tOrder.getSource()));
			this.setVisible(true);
		}
	}
}